// import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserProfile, login, getUserPhoto } from '@/api/user'
// import { resetRouter } from '@/router'

// const getDefaultState = () => {
//   return {
//     token: getToken(),
//     name: '',
//     avatar: ''
//   }
// }

const state = {
  token: getToken() || '',
  userinfo: {}
}

const mutations = {
  // RESET_STATE: (state) => {
  //   Object.assign(state, getDefaultState())
  // },
  setToken(state, newtoken) {
    state.token = newtoken
    setToken(newtoken)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  getUserInfo(state, userinfo) {
    state.userinfo = userinfo
  },
  removeUserInfo(state) {
    state.userinfo = {}
    // console.log(state.userinfo)
  }

}

const actions = {
  // 登录
  async loginUser(context, loginForm) {
    const res = await login(loginForm)
    // console.log(res)
    context.commit('setToken', res.data)
  },
  // 获取用户信息
  async getUserInfoActions(context) {
    const res = await getUserProfile()
    console.log(res)
    const res1 = await getUserPhoto(res.data.userId)
    console.log(res1)
    context.commit('getUserInfo', { ...res.data, ...res1.data })
  },
  // 退出登录
  logoutActions(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

